package com.xzy.sms.web.system.service;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.jboss.logging.Logger;

import com.xzy.sms.web.system.entity.Config;
import com.xzy.sms.web.system.utils.ConfigDef;

/**
 * 配置项key常量类
 * 
 * @author macong
 * @since 1.0
 * @version 2014-03-20 macong
 */
public class ConfigKeysConstants {
    
    /** log */
    private final static Logger log = Logger.getLogger(ConfigKeysConstants.class);
    
    /** 系统名称 */
    @ConfigDef(cnName = "系统名称 ", showType = "text", cnValue = "人品勋章", sort = 10, group = "system")
    public final static String SYSTEM_TITLE = "CSM_SYSTEM_TITLE";
    
    /** 系统名称 */
    @ConfigDef(cnName = "系统描述 ", showType = "text", cnValue = "深圳市乐牛网络科技有限公司", sort = 20, group = "system")
    public final static String SYSTEM_DESCRIPTION = "CSM_SYSTEM_DESCRIPTION";
    
    /**
     * 钱包服务号ID
     */
    @ConfigDef(cnName = "人品勋章对应的服务号ID ", showType = "text", cnValue = "", Desc = "人品勋章对应的服务号ID", sort = 30, group = "system")
    public static final String MONEY_PUB_SERVICE_ID = "MONEY_PUB_SERVICE_ID";
    
    /** 最大接入量 */
    @ConfigDef(cnName = "个人账户中的初始数量 ", showType = "number", cnValue = "0", Desc = "个人账户中的初始数量", sort = 40, group = "extend")
    public final static String MONEY_WALLET_INIT_BALANCE = "MONEY_WALLET_INIT_BALANCE";
    
    /**
     * 获取默认值
     * @param ckey ckey
     * @return str
     */
    public static String getCnValue(String ckey) {
        String val = null;
        Class<ConfigKeysConstants> classType = ConfigKeysConstants.class;
        Field[] fields = classType.getDeclaredFields();
        for (Field field : fields) {
            try {
                if (ckey.equals(String.valueOf(field.get(field.getName())))) {
                    if (field.isAnnotationPresent(ConfigDef.class)) {
                        ConfigDef annotation = field.getAnnotation(ConfigDef.class);
                        val = annotation.cnValue();
                    }
                }
            } catch (IllegalArgumentException e) {
                log.error(e);
            } catch (IllegalAccessException e) {
                log.error(e);
            }
        }
        return val;
    }
    
    /**
     * 获取备注
     * @param ckey ckey
     * @return str
     */
    public static String getRemarks(String ckey) {
        String val = null;
        Class<ConfigKeysConstants> classType = ConfigKeysConstants.class;
        Field[] fields = classType.getDeclaredFields();
        for (Field field : fields) {
            try {
                if (ckey.equals(String.valueOf(field.get(field.getName())))) {
                    if (field.isAnnotationPresent(ConfigDef.class)) {
                        ConfigDef annotation = field.getAnnotation(ConfigDef.class);
                        val = annotation.Desc();
                    }
                }
            } catch (IllegalArgumentException e) {
                log.error(e);
            } catch (IllegalAccessException e) {
                log.error(e);
            }
        }
        return val;
    }
    
    /**
     * 获取所有配置信息设置
     * @return Map
     */
    public static Map<Object, Config> getConfigInstance() {
        Map<Object, Config> map = new HashMap<Object, Config>();
        Class<ConfigKeysConstants> classType = ConfigKeysConstants.class;
        Field[] fields = classType.getDeclaredFields();
        for (Field field : fields) {
            boolean hasAnnotation = field.isAnnotationPresent(ConfigDef.class);
            if (hasAnnotation) {
                ConfigDef annotation = field.getAnnotation(ConfigDef.class);
                setConfig(map, field, annotation);
            }
        }
        return map;
    }
    
    /**
     * 设置Config
     * @param map map
     * @param field field
     * @param annotation annotation
     * @param type type
     */
    private static void setConfig(Map<Object, Config> map, Field field, ConfigDef annotation) {
        Config config = new Config();
        try {
            config.setCval(annotation.cnValue());
            config.setId(String.valueOf(field.get(field.getName())));
            config.setCnName(annotation.cnName());
            config.setShowType(annotation.showType());
            config.setSort(annotation.sort());
            config.setRemarks(annotation.Desc());
            config.setGroup(annotation.group());
            config.setDisplay(annotation.isDisplay());
            config.setIssued(annotation.issued());
            map.put(field.get(field.getName()), config);
        } catch (IllegalArgumentException e) {
            log.error(e);
        } catch (IllegalAccessException e) {
            log.error(e);
        }
    }
}
